<script lang="ts" setup>
import UserInfoForm from '@/components/form/UserInfoForm.vue'
import {
  queryUserInfoDetailOrderRequestData,
  refundRecordDetailOrderTableCol,
  queryUserInfoDetailOrderTableCol
} from '@/api/user/types/user'

import { queryUserInfoDetailOrder } from '@/api/user'
import { useTable } from '@/hooks/useTable'
import { useRoute, useRouter } from 'vue-router'
import { getToken } from '@/utils/cache/cookies'
import dayjs from 'dayjs'
import { PaymentType } from '@/types/enum'
import { reactive, onActivated } from 'vue'
const changeAccountType = (val: PaymentType) => {
  if (val == 'Alipay') {
    return '支付宝'
  } else if (val == 'WeChat') {
    return '微信'
  } else {
    return '其他'
  }
}
const route = useRoute()
const router = useRouter()
const handleFormSearch = () => {
  if (form.phone !== route.query.phone) {
    router.push({
      name: 'RefundRecord',
      query: {
        phone: form.phone,
        orderId: form.orderId,
        type: form.type,
        selectType: 'refund'
      }
    })
    return
  }
  setCurrentPage(1)
  load()
}

const form = reactive<queryUserInfoDetailOrderRequestData>({
  phone: route.query.phone as string,
  orderId: route.query.orderId as string,
  type: '',
  selectType: 'refund'
})
const { pagination, handleSizeChange, handleCurrentChange, data, loading, setCurrentPage, load } =
  useTable<queryUserInfoDetailOrderTableCol>(queryUserInfoDetailOrder, getToken() ?? '', () => form)
defineExpose({ load })
onActivated(() => {
  form.phone = route.query.phone as string
  form.orderId = route.query.orderId as string
})
</script>

<template>
  <user-info-form v-model="form" type="refund" @search="handleFormSearch">
    <template #select>
      <el-form-item prop="type" label="退款类型">
        <el-select v-model="form.type" style="width: 150px">
          <el-option value="" label="不限" />
          <el-option value="BikeLongTerm" label="车辆长租" />
          <el-option value="ChangeBattery" label="换电套餐" />
          <el-option value="BatteryActivity" label="电池套餐" />
          <el-option value="BatteryTimesCard" label="电池次卡" />
          <el-option value="BikeOverdue" label="租车逾期" />
          <el-option value="BatteryOverdue" label="电池逾期" />
          <el-option value="BatteryRerent" label="电池续租" />
          <el-option value="BikeRerent" label="车辆续租" />
          <el-option value="BikeOverdueRerent" label="租车逾期续租" />
          <el-option value="BatteryOverdueRerent" label="电池逾期续租" />
          <el-option value="bikeModified" label="中控线材(车辆改装)" />
          <el-option value="Battery" label="电池购买" />
          <el-option value="BikeDeposit" label="车辆押金" />
          <el-option value="BatteryDeposit" label="电池押金" />
          <el-option value="BikeMarginRefund" label="出区域保证金" />
        </el-select>
      </el-form-item>
    </template>
  </user-info-form>
  <el-table v-loading="loading" class="no-border" :data="data as refundRecordDetailOrderTableCol[]">
    <el-table-column prop="phone" label="手机号" min-width="150" align="center" />
    <el-table-column prop="orderId" label="充值订单号" min-width="250" align="center" />
    <el-table-column prop="refundType" label="退款类型" min-width="150" align="center" />
    <el-table-column prop="refundAmount" label="退款金额" align="center" />
    <el-table-column prop="accountType" label="退款路径" align="center">
      <template #default="scope">
        <span>{{ changeAccountType(scope.row.accountType) }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="refundOrderId" label="退款订单号" min-width="250" align="center" />
    <el-table-column prop="refundTime" label="退款时间" min-width="200" align="center">
      <template #default="scope">
        <span>{{ dayjs(scope.row.refundTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
      </template>
    </el-table-column>
    <el-table-column
      prop="reason"
      label="退款原因"
      min-width="200"
      align="center"
      show-overflow-tooltip
    />
  </el-table>
  <div flex justify-end mt-20>
    <el-pagination
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      v-model:current-page="pagination.pageNum"
      v-model:page-size="pagination.pageSize"
      background
      :total="pagination.total"
      layout="total,prev,pager,next,jumper,sizes"
    />
  </div>
</template>

<style scoped></style>
